<template>
    <div class="edit">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="权限">
                <el-checkbox-group v-model="permissions">
                    <el-checkbox :label="1">系管理</el-checkbox>
                    <el-checkbox :label="2">专业管理</el-checkbox>
                    <el-checkbox :label="4">班级管理</el-checkbox>
                    <el-checkbox :label="8">学生管理</el-checkbox>
                    <el-checkbox :label="16">教师管理</el-checkbox>
                    <el-checkbox :label="32">课程管理</el-checkbox>
                    <el-checkbox :label="64">选课修改</el-checkbox>
                    <el-checkbox :label="128">管理员管理</el-checkbox>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { adminAdd, getAdmin, adminEdit } from '@/api/admin';
export default {
    props: ['type', 'id'],
    data() {
        return {
            form: {
                id: -1,
                username: '',
                privilege: 0,
                password: '',
            },
            permissions: [],
            rules: {
                username: [
                    { require: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { require: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, message: '密码长度必须大于6位', trigger: 'blur' },
                ],
            }
        }
    },
    methods: {
        async getName(id) {
            if (this.type === 'edit') {
                let ret = await getAdmin(id);
                this.form = ret.data;
                for (let bit = 0; bit < 8; bit++) {
                    // 0100 0000
                    // 0010 0000
                    if (((1 << bit) & ret.data.privilege) === 1 << bit) {
                        this.permissions.push((1 << bit).toString());
                    }
                }
            }
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    this.form.privilege = 0
                    this.form.privilege = this.permissions.reduce((a, b) => a + b)
                    if (this.type === 'add') {
                        adminAdd(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                                this.permissions = []
                            }
                        })
                    } else if (this.type === 'edit') {
                        adminEdit(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                                this.permissions = []
                            }
                        })
                    }
                }
            });
        },
        cancel() {
            this.$emit('cancel')
        },
    },
    watch: {
        id: {
            handler(newVal, oldVal) {
                this.getName(newVal)
            },
            immediate: true
        }
    },
}
</script>